﻿/// <reference path="core/Error.js"/>
/// <reference path="FaScript.js"/>

"use strict";
Namespace('FaScript');

FaScript.defineClass('Event',
	function (name) {
	    var object = Object.create(null);

	    //私有变量
	    var _arrEvent = new Array();

	    //属性

	    //方法
	    Object.defineProperty(object, 'add', {
	        value: function (value) {
	            /// <summary>增加事件方法</summary>
	            /// <param name="value" type="Function" >处理方法</param>

	            _arrEvent.push(value);
	        }
	    });
	    Object.defineProperty(object, 'remove', {
	        value: function (value) {
	            /// <summary>移除事件方法</summary>
	            /// <param name="value" type="Function" >处理方法</param>

	            for (var i = 0, len = _arrEvent.length; i < len; ++i) {
	                if (_arrEvent[i].toString() === fn.toString()) {
	                    _arrEvent.splice(i, 1);
	                    break;
	                }
	            }
	        }
	    });
	    Object.defineProperty(object, 'trigger', {
	        value: function (sender) {
	            /// <summary>触发事件</summary>
	            /// <param name="sender" type="Object" >触发对象</param>

	            var args = arguments;
	            _arrEvent.forEach(function (fn) {
	                if (fn.apply(sender, args) === false) {
	                    return;
	                }
	            });
	        }
	    });

        //锁定成员
	    Object.seal(object);
	    return object;
	});